/* global Cesium viewer */
const P = Math.PI / 180;
const clock = new Cesium.Clock();
let timeImageryLayer = null;
let ICENTER_URL = 'http://192.168.30.112:8310';
clock.onTick.addEventListener(() => {
    viewer.clock.onTick.removeEventListener(clock.tick);
});

export function addImageryLayer(item, flag) {
    let imageryLayers = viewer.scene.imageryLayers;
    if (flag) {
        imageryLayers.add(
            new Cesium.ImageryLayer(
                new Cesium.C_WebMapTileServiceImageryProvider({
                    url: ICENTER_URL + '/geoserverplus/gwc/service/wmts',
                    layer: 'icenter:' + item.layerName,
                    style: 'default',
                    format: 'image/' + item.mimeType.toLowerCase(),
                    tileMatrixSetID: item.gridSet,
                    tilingScheme:
                        item.gridSet === 'EPSG:4326'
                            ? new Cesium.GeographicTilingScheme()
                            : new Cesium.WebMercatorTilingScheme(),
                    maximumLevel: item.maxLevel,
                }),
                {
                    minimumTerrainLevel: item.minLevel,
                    rectangle: new Cesium.Rectangle(
                        item.minX * P,
                        item.minY * P,
                        item.maxX * P,
                        item.maxY * P
                    ),
                }
            )
        );
    } else {
        for (let i = 0; i < imageryLayers.length; ++i) {
            if (imageryLayers._layers[i].imageryProvider._layer === 'icenter:' + item.layerName) {
                imageryLayers.remove(imageryLayers._layers[i]);
                break;
            }
        }
    }
}

export function changeClockTime(time) {
    if (!time) {
        throw new Error('请传入Iso8601格式的时间参数');
    }
    clock.currentTime = Cesium.JulianDate.fromIso8601(time);
    viewer.clock.onTick.addEventListener(clock.tick, clock);
}

export function addImageryLayerWithTime(flag, item = {}, params = {}) {
    let imageryLayers = viewer.scene.imageryLayers;
    imageryLayers.remove(timeImageryLayer);
    timeImageryLayer = null;
    if (flag) {
        if (!params || !item.layerName) {
            throw new Error('缺少必要的参数');
        }
        let timeRR = [...params];
        timeRR.push('9999-09-01');
        // eslint-disable-next-line new-cap
        let times = new Cesium.TimeIntervalCollection.fromIso8601DateArray({
            // iso8601Dates: params.times,
            iso8601Dates: timeRR,
            dataCallback: function dataCallback(interval) {
                return {
                    Time: Cesium.JulianDate.toIso8601(interval.start),
                };
            },
        });
        changeClockTime(params[0]);
        timeImageryLayer = new Cesium.ImageryLayer(
            new Cesium.C_WebMapTileServiceImageryProvider({
                url:
                    ICENTER_URL +
                    '/tilecache/service/time?request=gettile&yy={yy}&dd={dd}&mm={mm}&layer={layer}&zoo={TileMatrix}&row={TileCol}&col={TileRow}',
                layer: item.layerName,
                style: 'default',
                clock: clock,
                times: times,
                tileMatrixSetID: '',
                tilingScheme: new Cesium.GeographicTilingScheme(),
                maximumLevel: item.maxLevel,
            }),
            {
                minimumTerrainLevel: item.minLevel,
                rectangle: new Cesium.Rectangle(
                    item.minX * P,
                    item.minY * P,
                    item.maxX * P,
                    item.maxY * P
                ),
            }
        );
        imageryLayers.add(timeImageryLayer);
    }
}
